/**
 * 
 */
package com.google.code.maven.plugin.http.client;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * Proxy
 * 
 * @author Nadim Benabdenbi
 * @version 1.0
 * @since JDK1.6
 */
public class Proxy {

	/**
	 * @parameter
	 * @required
	 */
	private String host;

	/**
	 * @parameter default-value="8080"
	 */
	private int port = 8080;

	/**
	 * @parameter
	 */
	private Credentials credentials;

	/**
	 * host getter.
	 * 
	 * @return the host
	 */
	public String getHost() {
		return host;
	}

	/**
	 * host setter.
	 * 
	 * @param host the host to set
	 */
	public void setHost(String host) {
		this.host = host;
	}

	/**
	 * port getter.
	 * 
	 * @return the port
	 */
	public int getPort() {
		return port;
	}

	/**
	 * port setter.
	 * 
	 * @param port the port to set
	 */
	public void setPort(int port) {
		this.port = port;
	}

	/**
	 * credentials getter.
	 * 
	 * @return the credentials
	 */
	public Credentials getCredentials() {
		return credentials;
	}

	/**
	 * credentials setter.
	 * 
	 * @param credentials the credentials to set
	 */
	public void setCredentials(Credentials credentials) {
		this.credentials = credentials;
	}

	public void prepare(DefaultHttpClient httpclient) {
		HttpHost proxyHost = new HttpHost(host, port);
		httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost);
		if (credentials != null) {
			httpclient.getCredentialsProvider().setCredentials(//
					new AuthScope(host, port),//
					credentials.toUsernamePasswordCredentials());
		}
	}

	@Override
	public String toString() {
		return new StringBuilder("Proxy[url=").append(host).append(":").append(port).append(" authentication=").append(credentials != null).append("]").toString();
	}
}
